/*
 * pps_bom
 */
package com.pitop.product.ctrl;


import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

import com.pitop.core.exceptions.BaseException;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.product.entity.ProductBom;
import com.pitop.product.service.ProductBomSVImpl;

/**
 * 销售bom关联 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/productBom")
@Slf4j
@Api(value = "销售bom关联控制器", description = "销售bom关联控制器")
public class ProductBomCtrl {

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private ProductBomSVImpl productBomSVImpl;


    /**
     * 查询ProductBom一个详情信息
     *
     * @param id
     * @param productCode 成品编码
     * @param bomCode     bom编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询ProductBom一个详情信息", notes = "查询ProductBom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "productCode", value = "成品编码", dataType = "java.lang.String", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String productCode, String bomCode) {
        if (id == null) {
            return null;
        }
        if (productCode == null) {
            return null;
        }
        if (bomCode == null) {
            return null;
        }
        ProductBom productBom = productBomSVImpl.load(id, productCode, bomCode);
        log.info(JSON.toJSONString(productBom));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, productBom);
    }

    /**
     * 查询ProductBom一个详情信息
     *
     * @param id
     * @param productCode 成品编码
     * @param bomCode     bom编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询ProductBom一个详情信息", notes = "查询ProductBom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "productCode", value = "成品编码", dataType = "java.lang.String", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/get")
    @ResponseBody
    public BeanRet get(Long id, String productCode, String bomCode) {
        if (id == null) {
            return null;
        }
        if (productCode == null) {
            return null;
        }
        if (bomCode == null) {
            return null;
        }
        ProductBom productBom = productBomSVImpl.get(id, productCode, bomCode);
        log.info(JSON.toJSONString(productBom));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, productBom);
    }


    /**
     * 根据条件productCode查询ProductBom一个详情信息
     *
     * @param productCode 成品编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询ProductBom一个详情信息", notes = "查询ProductBom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productCode", value = "成品编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/productCode/{productCode}")
    @ResponseBody
    public BeanRet loadByProductCode(@PathVariable String productCode) {
        if (StringUtils.isEmpty(productCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        ProductBom productBom = productBomSVImpl.loadByProductCode(productCode);
        log.info(JSON.toJSONString(productBom));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, productBom);
    }


    /**
     * 根据条件productCode查询ProductBom一个详情信息
     *
     * @param productCode 成品编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询ProductBom一个详情信息", notes = "查询ProductBom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productCode", value = "成品编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/productCode/{productCode}")
    @ResponseBody
    public BeanRet getByProductCode(@PathVariable String productCode) {
        if (StringUtils.isEmpty(productCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        ProductBom productBom = productBomSVImpl.getByProductCode(productCode);
        log.info(JSON.toJSONString(productBom));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, productBom);
    }


    /**
     * 查询ProductBom信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询ProductBom信息集合", notes = "查询ProductBom信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),

    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore ProductBom productBom, @ApiIgnore Page<ProductBom> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<ProductBom> productBoms = productBomSVImpl.list(productBom, page.getCurPage(), page.getPageSize());
        int total = productBomSVImpl.count(productBom);
        page.setTotalRow(total);
        page.setVoList(productBoms);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }


    /**
     * 创建ProductBom
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建ProductBom", notes = "创建ProductBom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productCode", value = "成品编码", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码,多个以逗号“,”分割开", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore ProductBom productBom) {
        if (StringUtils.isEmpty(productBom.getProductCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(productBom.getBomCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        //检测是否重复
        String[] bomCodeAry = productBom.getBomCode().split(",");
        for (String bomCode : bomCodeAry) {
            if (productBomSVImpl.load(null, productBom.getProductCode(), bomCode) != null) {
                return BeanRet.create(false, "项目" + productBom.getProductCode() + "已关联bom:" + bomCode);
            }
        }
        productBomSVImpl.save(productBom);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, productBom);
    }

    /**
     * 修改ProductBom
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改ProductBom", notes = "修改ProductBom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "productCode", value = "成品编码", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore ProductBom productBom) {
        if (StringUtils.isEmpty(productBom.getProductCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(productBom.getBomCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        productBomSVImpl.modify(productBom);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, productBom);
    }

    /**
     * 根据主键删除ProductBom
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除ProductBom", notes = "删除ProductBom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "productCode", value = "成品编码", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String productCode, String bomCode) {
        if (id != null && StringUtils.isEmpty(productCode) && StringUtils.isEmpty(bomCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }

        productBomSVImpl.delete(id, productCode, bomCode);
        return BeanRet.create(true, "删除ProductBom成功");
    }

}
